# _*_ coding: utf-8 _*_
import requests
from lxml.etree import HTML


class CatalogSpider(object):
    '''小说简易目录爬虫'''
    def __init__(self):
        self.__url = None
        self.__response = None
        self.title = None
        self.results = []
        self.length = 0


    @property
    def url(self):
        return self.__url


    @url.setter
    def url(self, val: str):
        self.__url = val


    def setHeaders(self) -> dict:
        '''设置请求体'''
        return {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"
        }


    def send(self) -> None:
        '''发送请求'''
        response = requests.get(
            url=self.url,
            headers=self.setHeaders()
        )
        response.encoding = "utf-8"
        self.__response = response


    def parse(self) -> None:
        '''解析响应体'''
        root = HTML(self.__response.text)
        # 获得小说标题
        self.title = root.xpath('''//div[@id="info"]/h1/text()''')[0]

        index = 0
        for index, dd in enumerate(root.xpath('''//div[@id="list"]/dl/dd''')):
            novelTitle = dd.xpath("./a/text()")[0]
            novelURL = f"https://www.xbiquge.la" + dd.xpath("./a/@href")[0]
            self.results.append((index, novelTitle, novelURL))
        else:
            self.length = index + 1


if __name__ == "__main__":
    pass